package com.hqyj.cl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.cl.mapper.UserMapper;
import com.hqyj.cl.pojo.User;
import com.hqyj.cl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description：
 * @author：cc
 * @date：2021/10/12 15:43
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    // login
    @Override
    public Map<String, String> login(User user) {
        // 查询用户
        User loginUser = userMapper.login(user);
        // 创建一个Map集合对象
        Map<String, String> map = new HashMap<>();
        // 做判断，判断用户是否存在
        if (loginUser == null){
            map.put("loginInfo","该用户账号或者密码错误");
            map.put("code","201");
            return map;
        }else {
            map.put("loginInfo","该用户存在");
            map.put("code","200");
            return map;
        }
    }

    @Override
    public List<User> findAll() {
        return  userMapper.findAll();
    }

    // queryAll
    @Override
    public PageInfo<User> queryAll(int PageNum, int PageSize) {
        // 使用PageHelper插件分页
        // 开启分页
        PageHelper.startPage(PageNum, PageSize);
        // 查询数据库，获取查询数据
        List<User> users = userMapper.queryAll();
        // 将查询数据封装到PageInfo对象中
        /*// 输出PageInfo
        System.out.println(userPageInfo);*/
        return new PageInfo<>(users);
    }
}
